<?php
/**
 * Created by PhpStorm.
 * User: 68008000
 * Date: 2017/11/23
 * Time: 15:48
 */

namespace app\common\model;


use think\Controller;
use think\Loader;
use think\Model;

/**
 * Class Goods
 * @package app\common\model
 * 商品管理表控制器
 */
class Goods extends Model
{
    //连接数据库
    protected $table= 'shop_goods';
    protected $pk= 'gid';

    protected $auto = ['time'];
    protected function setTimeAttr()
    {
        return time();
    }
    //商品添加
    public function add($data)
    {
        //1商品表的添加
        $result = $this->validate(true)->allowField(true)->save($data);
        if (false == $result) {
            return ['valid' => 0, 'msg' => $this->getError()];
        } else {
            //2商品详情表的添加
            //商品详情表的图片处理
            $bigPath = '';
            $mediumPath = '';
            $smallPath = '';
            foreach ($data['img'] as $k => $v) {
                $v = strchr($v, 'uploads');
                $image = \think\Image::open($v);
                //保存大图
                $big = dirname($v) . '/big_' . basename($v);
                $image->thumb(800, 800)->save($big);
                //保存中图
                $medium = dirname($v) . '/medium_' . basename($v);
                $image->thumb(400, 400)->save($medium);
                //保存小图
                $small = dirname($v) . '/small_' . basename($v);
                $image->thumb(100, 100)->save($small);
                //处理图片路径1
                $bigPath .= HD_ROOT . $big . ',';
                $mediumPath .= HD_ROOT . $medium . ',';
                $smallPath .= HD_ROOT . $small . ',';
            }
            //处理图片路径2
            $bigPath = rtrim($bigPath, ',');
            $mediumPath = rtrim($mediumPath, ',');
            $smallPath = rtrim($smallPath, ',');
            //保存数据库
            $pogData = [
                'big' => $bigPath,
                'medium' => $mediumPath,
                'small' => $smallPath,
                'pog' => $data['pog'],
                'service' => $data['service'],
                'gid'=>$this->gid,
            ];
            (new Pog())->save($pogData);
            //3.商品属性表的验证和添加
            //处理属性数据
            foreach ($data['attr'] as $k=>$v){
                if($v){
                    $attrData = [
                        'gtvalue'=>$v,
                        'added'=>0,
                        'taid'=>$k,
                        'gid'=>$this->gid,
                    ];
                    (new GoodsAttribute())->save($attrData);
                }

            }
            //处理规格数据
            foreach ($data['spec'] as $k=>$v){
                foreach ($v['color'] as $kk=>$vv){
                    $specData = [
                        'gtvalue'=>$vv,
                        'added'=>$v['added'][$kk],
                        'taid'=>$k,
                        'gid'=>$this->gid,
                    ];
                    (new GoodsAttribute())->save($specData);
                }
            }
            (new Pog())->save($pogData);
            return ['valid'=>1,'msg'=>'添加成功'];
        }
    }
    //商品编辑
    public function edit($data){
        p($data);
        $gid = input('param.gid');
        //1商品表的添加
        $result = $this->validate(true)->allowField(true)->save($data,['gid' => $gid]);
        if (false == $result) {
            return ['valid' => 0, 'msg' => $this->getError()];
        } else {
            //2商品详情表的添加
            //商品详情表的图片处理
            $bigPath = '';
            $mediumPath = '';
            $smallPath = '';
            foreach ($data['img'] as $k => $v) {
                $v = strchr($v, 'uploads');
                $image = \think\Image::open($v);
                //保存大图
                $big = dirname($v) . '/big_' . basename($v);
                $image->thumb(800, 800)->save($big);
                //保存中图
                $medium = dirname($v) . '/medium_' . basename($v);
                $image->thumb(400, 400)->save($medium);
                //保存小图
                $small = dirname($v) . '/small_' . basename($v);
                $image->thumb(100, 100)->save($small);
                //处理图片路径1
                $bigPath .= HD_ROOT . $big . ',';
                $mediumPath .= HD_ROOT . $medium . ',';
                $smallPath .= HD_ROOT . $small . ',';
            }
            //处理图片路径2
            $bigPath = rtrim($bigPath, ',');
            $mediumPath = rtrim($mediumPath, ',');
            $smallPath = rtrim($smallPath, ',');
            //保存数据库
            $pogData = [
                'big' => $bigPath,
                'medium' => $mediumPath,
                'small' => $smallPath,
                'pog' => $data['pog'],
                'service' => $data['service'],
                'gid'=>$gid,
            ];
            (new Pog())->save($pogData,['gid' => $gid]);
            //3.商品属性表的验证和添加
            //处理属性数据
            db('goods-attribute')->where('gid',$gid)->delete();
            foreach ($data['attr'] as $k=>$v){
                if($v){
                    $attrData = [
                        'gtvalue'=>$v,
                        'added'=>0,
                        'taid'=>$k,
                        'gid'=>$gid,
                    ];
                    (new GoodsAttribute())->save($attrData);
                }

            }
            //处理规格数据
            db('goods-attribute')->where('gid',$gid)->delete();
            foreach ($data['spec'] as $k=>$v){
                foreach ($v['color'] as $kk=>$vv){
                    $specData = [
                        'gtvalue'=>$vv,
                        'added'=>$v['added'][$kk],
                        'taid'=>$k,
                        'gid'=>$gid,
                    ];
                    (new GoodsAttribute())->save($specData);
                }
            }
            (new Pog())->save($pogData);
            return ['valid'=>1,'msg'=>'添加成功'];
        }
    }
    //删除
    public function del($gid){
        //1.通过当前要删除的数据的tid获取当前数据的tid
        db('pog')->where('gid',$gid)->select();
        db('goods-attribute')->where('gid',$gid)->select();
        $res =db('goods')->delete($gid);
        if ($res){
            db('pog')->where('gid',$gid)->delete();
            db('goods-attribute')->where('gid',$gid)->delete();
            return ['valid'=>1,'msg'=>'删除成功'];
        }else{
            return ['valid'=>0,'msg'=>'删除失败'];
        }
    }

}